package com.joker.web.controller.orange;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

import cn.hutool.core.util.ObjectUtil;
import com.joker.common.core.domain.entity.SysDictData;
import com.joker.orange.domain.NovelInfo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.joker.common.annotation.RepeatSubmit;
import com.joker.common.annotation.Log;
import com.joker.common.core.controller.BaseController;
import com.joker.common.core.domain.PageQuery;
import com.joker.common.core.domain.R;
import com.joker.common.core.validate.AddGroup;
import com.joker.common.core.validate.EditGroup;
import com.joker.common.enums.BusinessType;
import com.joker.common.utils.poi.ExcelUtil;
import com.joker.orange.domain.vo.NovelInfoVo;
import com.joker.orange.domain.bo.NovelInfoBo;
import com.joker.orange.service.INovelInfoService;
import com.joker.common.core.page.TableDataInfo;

/**
 * 小说信息
 *
 * @author Joker
 * @date 2024-06-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pixiv/novel")
public class NovelInfoController extends BaseController {

    private final INovelInfoService iNovelInfoService;

    /**
     * 查询小说信息列表
     */
    @SaCheckPermission("pixiv:novel:list")
    @GetMapping("/list")
    public TableDataInfo<NovelInfoVo> list(NovelInfoBo bo, PageQuery pageQuery) {
        return iNovelInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出小说信息列表
     */
    @SaCheckPermission("pixiv:novel:export")
    @Log(title = "小说信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(NovelInfoBo bo, HttpServletResponse response) {
        List<NovelInfoVo> list = iNovelInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "小说信息", NovelInfoVo.class, response);
    }

    /**
     * 获取小说信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("pixiv:novel:query")
    @GetMapping("/{id}")
    public R<NovelInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iNovelInfoService.queryById(id));
    }

    @GetMapping("/getNovelInfo/{id}")
    public R<NovelInfo> getNovelInfo(@NotNull(message = "主键不能为空")
                                  @PathVariable Long id) {
        return R.ok(iNovelInfoService.getNovelInfo(id));
    }

    @GetMapping("/listAllNovel")
    public R<List<NovelInfo>> listAllNovel() {
        return R.ok(iNovelInfoService.listAllNovel());
    }

    /**
     * 新增小说信息
     */
    @SaCheckPermission("pixiv:novel:add")
    @Log(title = "小说信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody NovelInfoBo bo) {
        return toAjax(iNovelInfoService.insertByBo(bo));
    }

    /**
     * 修改小说信息
     */
    @SaCheckPermission("pixiv:novel:edit")
    @Log(title = "小说信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody NovelInfoBo bo) {
        return toAjax(iNovelInfoService.updateByBo(bo));
    }

    /**
     * 删除小说信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("pixiv:novel:remove")
    @Log(title = "小说信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iNovelInfoService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
